<?php
namespace app\common\model;

use app\extra\BaseModel;
use app\extra\DBCont;
use think\Db;
use think\Exception;

class VoteInfo extends BaseModel
{
    // 开始时间
//    public function setStartTimeAttr($value){
//        return strtotime($value);
//    }
//    public function getStartTimeAttr($value){
//        return date('Y-m-d',$value);
//    }
    // 结束时间
//    public function setEndTimeAttr($value){
//        return strtotime($value);
//    }
//    public function getEndTimeAttr($value){
//        return date('Y-m-d',$value);
//    }
    // 图片
    public function setImagesAttr($value){
        if (is_array($value)){
            $arr = [];
            foreach ($value as $item){
                if (!empty($item)){
                    $arr[] = $item;
                }
            }
            return serializeByArray($arr, ',');
        }else {
            return $value;
        }

    }
    public function getImagesAttr($value){
        $data = unSerializeByJson($value, ',');
        $arr = [];
        foreach ($data as $v){
            $arr[] = [
                'key' => $v,
                'value' => showFileUrl($v)
            ];
        }
        return $arr;
    }
    /**
     * 添加额外参数
     * @param $item
     */
    public function setAddedField(&$item){
        $item['player_count'] = model('Player')->where('vote_id', $item['id'])->count();
        $item['vote_sum'] = model('Player')->where('vote_id', $item['id'])->sum('init_vote');
        $item['vote_sum'] += model('Player')->where('vote_id', $item['id'])->sum('poll');
        $item['vote_sum'] += $item['player_count'] * $item['init_vote'];
        $item['page_view'] = model('Player')->where('vote_id', $item['id'])->sum('page_view');
        $item['status_desc'] = $item['status'];
        if ($item['status'] == DBCont::SUCCESS_DESC){
            if($item['start_time'] > time()){
                $item['status_desc'] = DBCont::WAIT_DESC;
            }elseif($item['end_time'] < time()){
                $item['status_desc'] = DBCont::CANCEL_DESC;
            }
        }
    }
}